package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.CollectMapper;
import com.springboot_gradrecruit.mapper.JobMapper;
import com.springboot_gradrecruit.model.dto.collect.PageCollectRequest;
import com.springboot_gradrecruit.model.entity.Collect;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.service.CollectService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zr
* @description 针对表【collect】的数据库操作Service实现
* @createDate 2024-11-08 09:52:58
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService {

    @Resource
    private CollectMapper collectMapper;


    @Resource
    private JobMapper jobMapper;

    @Override
    public PageResponse<JobVO> pageCollect(PageCollectRequest pageCollectRequest) {
        //1.获取参数
        Long studentId=pageCollectRequest.getStudentId();
        String jobLocation = pageCollectRequest.getJobLocation();
        String jobIndustry = pageCollectRequest.getJobIndustry();
        String searchName = pageCollectRequest.getSearchName();

        //2.构建查询条件，查询学生收藏的所有职位
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        List<Collect> collects = collectMapper.selectList(queryWrapper);
        if (collects.isEmpty()){
            return PageResponse.of(new Page<>(), JobVO.class);
        }
        List<Long> jobIds = collects.stream().map(Collect::getJobId).toList();
        // 3.根据职位id查询职位信息
        LambdaQueryWrapper<Job> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Job::getJobId, jobIds)
                .like(StrUtil.isNotBlank(jobIndustry), Job::getJobIndustry, jobIndustry)
                .like(StrUtil.isNotBlank(jobLocation), Job::getJobLocation, jobLocation)
                .and(StrUtil.isNotBlank(searchName), w -> w.like(Job::getJobTitle, searchName)
                        .or()
                        .like(Job::getJobCompanyName, searchName));
        List<Job> jobs = jobMapper.selectList(lambdaQueryWrapper);


        // 4.将职位信息封装到 JobVO 对象中
        List<JobVO> jobVOS = jobs.stream().map(job -> {
            JobVO jobVO = BeanUtil.copyProperties(job, JobVO.class);
            return jobVO;

        }).toList();

        // 5.构建分页响应并返回
        PageResponse<JobVO> pageResponse = new PageResponse<>();
        pageResponse.setData(jobVOS);
        pageResponse.setTotal((long) jobVOS.size()); // 这里假设总记录数等于当前页记录数，可以根据实际需求调整
        long totalPages = (long) Math.ceil((double) jobVOS.size() / pageCollectRequest.getPageSize());
        pageResponse.setPages(totalPages);
        return pageResponse;

    }
}




